package com.plant.action;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import com.plant.entity.UgcComplaint;
import com.plant.entity.UgcFeedback;
import com.plant.service.NewThingsService;
/**
 *  @Table(name="pgc_topic",catalog="mr_plants")
 * 			  --secret	由 随机的字符串 和 密匙（token） 组成进行md5加密组成
				---	pust_val	随机生成的字符串。可以是时间戳或乱码字符
 *
 */
@Controller
@RequestMapping(value="/feedback")
public class UgcFeedBackAction {
	
	@Resource(name = "newThingsServiceImpl")
    private NewThingsService newThingsService;
	
	
	@RequestMapping(value="/newfeedback")  //----1.发送反馈接口
	@ResponseBody
	public Map<Object,Object> NewFeedBack(UgcFeedback ufb,HttpServletResponse response,String secret,String pust_val,HttpServletRequest request){
		response.setHeader("access-control-allow-origin","*");
		Map<Object,Object> map=new HashMap<Object, Object>();
		if(secret==null||pust_val==null||ufb.getUserId()==null){
			map.put("message", "参数为空");
			map.put("state", 0);
			return map;
		}else{
				Md md=new Md();
				String mdsc=md.md5calc(pust_val+"flower_duigu2016"); 
				if(secret.endsWith(mdsc)){
						if(ufb.getFeedback()==null){
							map.put("message", "输入内容不能为空");
							map.put("state", 0);
							return map;
						}else{
							GetIp getip=new GetIp();
							String ip=getip.getIp2(request);
							SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
							String t=sim.format(new Date());
							ufb.setIp(ip);//---获取发送用户的IP地址
							ufb.setCreateTime(Timestamp.valueOf(t));//--创建发送时间
//							BASE64Encoder base64Encode=new BASE64Encoder();
//							String content=base64Encode.encode(ufb.getFeedback().getBytes()); 
//							ufb.setFeedback(content);
							boolean b=newThingsService.AddFeedBack(ufb);
							if(b){
								map.put("message", "发送反馈信息成功");
								map.put("state", 1);
								return map;	
							}else{
								map.put("message", "发送反馈信息失败,请检查网络");
								map.put("state", 0);
								return map;
							}
						}
				}else{
					map.put("message", "秘钥错误");
					map.put("state", 0);
					return map;	
				}
		}
	}
	
	
	@RequestMapping(value="/newcomplaint")  //----2.投诉接口
	@ResponseBody
	public Map<Object,Object> NewComplaint(Integer user_id,Integer to_user_id,Integer to_content_id,Integer to_content_type,UgcComplaint ucp,HttpServletResponse response,String secret,String pust_val,HttpServletRequest request){
		response.setHeader("access-control-allow-origin","*");
		Map<Object,Object> map=new HashMap<Object, Object>();
		if(secret==null||pust_val==null||user_id==null||to_user_id==null||to_content_id==null||to_content_type==null){
			map.put("message", "参数为空");
			map.put("state", 0);
			return map;
		}else{
			Md md=new Md();
			String mdsc=md.md5calc(pust_val+"flower_duigu2016"); 
			if(secret.endsWith(mdsc)){
						ucp.setUserId(user_id);
						ucp.setToUserId(to_user_id);
						ucp.setToContentId(to_content_id);
						ucp.setToContentType(to_content_type);
						if(ucp.getContent()==null){
							map.put("message", "投诉内容为空");
							map.put("state", 0);
							return map;
						}else{
							GetIp getip=new GetIp();
							String ip=getip.getIp2(request);
							SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
							String t=sim.format(new Date());
							ucp.setIp(ip);//---获取ip地址
							ucp.setCreateTime(Timestamp.valueOf(t));//--投诉时间
							boolean b=newThingsService.AddComplaint(ucp);
							if(b){
								map.put("message", "投诉成功 正在审核");
								map.put("state", 1);
								return map;	
							}else{
								map.put("message", "投诉失败 查看网络");
								map.put("state", 0);
								return map;
							}
						}
			}else{
				map.put("message", "秘钥错误");
				map.put("state", 0);
				return map;	
			}
		}
	}
	
}
